<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${mapper.packageName}.${entity.name}${mapperSuffix}">

	<!-- =================================对象映射============================== -->
	
	<resultMap id="${entity.name}Map" type="${entity.fullName}">
        <#list table.fields as field>
            <#if field.name == 'id' >
        <id property="id" column="id" jdbcType="${field.jdbcType}"/>
            </#if>
            <#if field.name != 'id' >
        <result property="${field.javaName}" column="${field.name}" jdbcType="${field.jdbcType}"/>
            </#if>
        </#list>
	</resultMap>

	<!-- ===================================字段集============================== -->
	
	<sql id="columns">
        <#list table.fields as field>
            <#if field_has_next>
        ${field.name},
            <#else>
        ${field.name}
            </#if>
        </#list>
	</sql>

	<!-- ===================================属性集============================= -->

	<!-- 单条插入 -->
	<sql id="insertValues">
        <#list table.fields as field>
            <#if field.name == 'version'>
                <#if field_has_next>
        0,
                <#else>
        0
                </#if>
            <#else>
                <#if field_has_next>
        <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}},
                <#else>
        <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}}
                </#if>
            </#if>
        </#list>
	</sql>

	<!-- 批量插入 -->
	<sql id="batchInsertValues">
        <#list table.fields as field>
            <#if field.name == 'version'>
                <#if field_has_next>
        0,
                <#else>
        0
                </#if>
            <#else>
                <#if field_has_next>
        <#noparse>#{</#noparse>item.${field.javaName},jdbcType=${field.jdbcType}},
                <#else>
        <#noparse>#{</#noparse>item.${field.javaName},jdbcType=${field.jdbcType}}
                </#if>
            </#if>
        </#list>
    </sql>

    <!-- 单条更新 -->
    <sql id="updateSet">
        <#list table.fields as field>
            <#if field.name != 'id'>
                <#if field.name == 'version'>
                    <#if field_has_next>
            version = version+1,
                    <#else>
            version = version+1
                    </#if>
                 <#else>
                    <#if field_has_next>
        <if test="${field.javaName} != null">
            ${field.name} = <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}},
        </if>
                    <#else>
        <if test="${field.javaName} != null">
            ${field.name} = <#noparse>#{</#noparse>${field.javaName},jdbcType=${field.jdbcType}}
        </if>
                    </#if>
                </#if>
            </#if>
        </#list>
    </sql>

	<!-- ===================================条件集============================= -->

	<sql id="conditionWhere">
        <#list table.fields as field>
        <if test="@${mapper.ognlClass}@isNotEmpty(${r'${condition}'}${field.javaName})">
            AND ${field.name} = <#noparse>#{</#noparse>${r'${condition}'}${field.javaName},jdbcType=${field.jdbcType}}
        </if>
            <#if field.isDateType == '1'>
        <if test="@${mapper.ognlClass}@isNotEmpty(${r'${condition}'}start${field.javaBigName})">
            <![CDATA[ AND ${field.name} >= <#noparse>#{</#noparse>${r'${condition}'}start${field.javaBigName},jdbcType=VARCHAR} ]]>
        </if>
        <if test="@${mapper.ognlClass}@isNotEmpty(${r'${condition}'}end${field.javaBigName})">
            <![CDATA[ AND ${field.name} < <#noparse>#{</#noparse>${r'${condition}'}end${field.javaBigName},jdbcType=VARCHAR} ]]>
        </if>
            </#if>
        </#list>
        <if test="@${mapper.ognlClass}@isNotEmpty(${r'${condition}'}ids)">
            <#list table.fields as field>
                <#if field.name == 'id' >
            AND id IN
            <foreach collection="${r'${condition}'}ids" item="item" index="index" separator="," open="(" close=")">
                <#noparse>#{</#noparse>item,jdbcType=${field.jdbcType}}
            </foreach>
                </#if>
            </#list>
        </if>

        <#if entity.hasName || entity.hasTitle || entity.idAttr.type == 'String'>
        <!-- 模糊查询 -->
        </#if>
        <#if entity.idAttr.type == 'String' >
        <if test="@cn.demoncat.util.mybatis.util.MapperOgnl@isNotEmpty(${r'${condition}'}idLike)">
            AND id LIKE CONCAT(<#noparse>#{</#noparse>${r'${condition}'}idLike,jdbcType=VARCHAR}, '%')
        </if>
        </#if>
        <#if entity.hasName >
        <if test="@cn.demoncat.util.mybatis.util.MapperOgnl@isNotEmpty(${r'${condition}'}nameLike)">
            AND name LIKE CONCAT('%', <#noparse>#{</#noparse>${r'${condition}'}nameLike,jdbcType=VARCHAR}, '%')
        </if>
        </#if>
        <#if entity.hasTitle >
        <if test="@cn.demoncat.util.mybatis.util.MapperOgnl@isNotEmpty(${r'${condition}'}titleLike)">
            AND title LIKE CONCAT('%', <#noparse>#{</#noparse>${r'${condition}'}titleLike,jdbcType=VARCHAR}, '%')
        </if>
        </#if>
	</sql>

	<!-- ===================================批量插入============================ -->

    <insert id="batchSave" useGeneratedKeys="true" keyProperty="id" parameterType="java.util.List">
        INSERT INTO ${table.name}(
            <include refid="columns"/>
        ) VALUES
            <foreach collection="list" item="item" index="index" separator=",">
                (<include refid="batchInsertValues"/>)
            </foreach>
    </insert>

	<!-- ==================================自定义搜索=========================== -->

    <select id="select" resultType="${entity.fullName}">
        SELECT ${r'${column}'}
        <if test="child == null">
        FROM ${table.name}
        </if>
        <if test="child != null">
        FROM (
            SELECT ${r'${child.column}'}
            FROM ${table.name}
            <where>
                <include refid="conditionWhere">
                    <property name="condition" value="child.condition."/>
                </include>
            </where>
            ${r'${child.group}'} ${r'${child.order}'} ${r'${child.limit}'}
        ) child
        </if>
        <where>
            <include refid="conditionWhere">
                <property name="condition" value="condition."/>
            </include>
        </where>
        ${r'${group}'} ${r'${order}'} ${r'${limit}'}
    </select>


	<!-- ==================================更新日志============================ -->
    <!-- 
        ${entity.time}  ${entity.author}    初始化备用SQL

     -->
     
</mapper>